// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

// standard headers

/*
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdarg>
#include <stdexcept>
#include <utility>
#include <tuple>

#include <cmdioutil.h>
#include "xyz.h"
*/
#include "param_interpret.h"  

int main(int argc, char** argv)
{
try
{   
	cmd::arguments arg(argc,argv);
	cmd::switcharg<false> help(arg,"-h","--help", "print help message");

        xyz c;

        /////////////////////////////////////////////////////////////////////////
        //cmd::multivararg<std::string> selected_range (arg,"-s","--select","(repeatable opt) select atoms, by default all selected");
        //cmd::switcharg<false> invert_selection(arg,"-i","--invert-selection", "invert selection range");
        //////////////////////////////////////////////////////////////////////////


        cmd::vararg<std::string> read_d (arg,"-d","--dist","length of the @1 vector (distance of 2 points)","");
//        cmd::vararg<std::string> read_dx (arg,"-dx","--dx","x comp. of @1 vector","");
//        cmd::vararg<std::string> read_dy (arg,"-dy","--dy","y comp. of @1 vector","");
//        cmd::vararg<std::string> read_dz (arg,"-dz","--dz","z comp. of @1 vector","");
        cmd::vararg<std::string> read_x (arg,"-x","--x","x comp. of @1 vector or coord","");
        cmd::vararg<std::string> read_y (arg,"-y","--y","y comp. of @1 vector or coord","");
        cmd::vararg<std::string> read_z (arg,"-z","--z","z comp. of @1 vector or coord","");
        cmd::vararg<std::string> read_v (arg,"-v","--v","out @1 vector or coord in x/y/z form","");

                                                                                                                                                                                                                              

	if(*help)
	{
		arg.print_help(std::cout,"xyzread");
                std::cout << std::endl;
                std::cout << "Version 0.0"<< std::endl;
                std::cout << "Copyright (c) 2010 David Zsolt Manrique (david.zsolt.manrique@gmail.com)"<< std::endl;
            	return EXIT_SUCCESS;
	}

        cmd::posarg<std::string> io_fn(arg,"input file name", "");
        std::ifstream is((*io_fn).c_str());
        load(is,c);
        is.close();

        ///////////////////////////////////////////////////////////////////////////
        //if(~selected_range) for(int i=0;i<(*selected_range).size();i++) select_by_expression(c,(*selected_range)[i]); else neg(c);
        //if(*invert_selection) neg(c);
        //////////////////////////////////////////////////////////////////////////


        if(~read_d) {                                                                                                                                                                     
            std::cout << interpret_coord(c,*read_d).norm() << std::endl;
        } else if (~read_x) {
            std::cout << interpret_coord(c,*read_x).x() << std::endl;
        } else if (~read_y) {
            std::cout << interpret_coord(c,*read_y).y() << std::endl;
        } else if (~read_z) {
            std::cout << interpret_coord(c,*read_z).z() << std::endl;
        } else if (~read_v) {
            std::cout << interpret_coord(c,*read_v).x() <<'/' << interpret_coord(c,*read_v).y() << '/' << interpret_coord(c,*read_v).z()<< std::endl; //this is ridiculusly inefficient.. :)
        }

//        std::cerr << "not yet!" << std::endl;


        ////////////////////////////////////////////////////
        
} catch(const char* message)
{
	std::cerr << message << std::endl;
	std::cerr << "(exit)" << std::endl;
	return EXIT_FAILURE;
} 
catch(const std::exception & e)
{
        std::cerr << e.what() << std::endl;
        std::cerr << "(exit)" << std::endl;
        return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}


